package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import static com.shanxia.codec.Decoder.getUint16;
import static com.shanxia.codec.Decoder.getUint8;

/**
 * C -> S
 * 充电桩应答服务器下发获取充电桩日志命令
 */
@Data
public class Cmd1407 extends ProtocolDataBase {

    public static final int MIN_SIZE = 5;

    private int v1;
    private int v2;
    private int code;                   // 结果

    public Cmd1407(){
        super.setCmd(1407);
    }

    public static Cmd1407 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd1407 cmd = new Cmd1407();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        cmd.v1 = getUint16(dataField, offset);
        offset += 2;
        cmd.v2 = getUint16(dataField, offset);
        offset += 2;
        // 1字节结果
        cmd.code = getUint8(dataField[offset]);
        return cmd;
    }

    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append("#cmd1407#").append("\n");
        buffer.append("v1:").append(v1).append("\n");
        buffer.append("v2:").append(v2).append("\n");
        buffer.append("结果:").append(code).append("\n");
        return buffer.toString();
    }
}